package com.etc.controller;

import com.etc.pojo.User;
import com.etc.pojo.Video;
import com.etc.service.VideoPersonageService;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.UUID;

@Controller
@RequestMapping("VideoPersonageController")
public class VideoPersonageController {

    @Autowired
    VideoPersonageService videoService;

    @ResponseBody
    @RequestMapping(value = "queryAllVideoByUserId")
    public PageInfo<Video> queryAllVideoByUserId(@RequestParam(value = "pageNum", defaultValue = "1")  Integer pageNum,
                                                 HttpSession session) {
        User user = (User)session.getAttribute("user");
        PageInfo<Video> pageInfo = videoService.queryAllVideoByUserId(user.getUserId(),pageNum);
        return pageInfo;
    }

    @ResponseBody
    @RequestMapping(value = "deleteVideoByVideoId")
    public boolean deleteVideoByVideoId(@RequestParam(value = "videoId")  String videoId, @RequestParam(value = "videoState")  int videoState) {
        boolean leep = videoService.deleteVideoByVideoId(videoId,videoState);
        return leep;
    }

    @RequestMapping(value = "addVideo")
    public ModelAndView addVideo(Video video,
                                 @RequestParam("videoImgv") MultipartFile videoImgv,
                                 @RequestParam("videoPathv") MultipartFile videoPathv,
                                 HttpSession session) throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        User user = (User)session.getAttribute("user"); //获取用户
        String videoImgPath = videoImgv.getOriginalFilename(); //获取头像名
        String imgPath = UUID.randomUUID().toString() + videoImgPath;
        File file = new File("e:\\exercisefigure\\" + imgPath);
        String videoPathn = videoPathv.getOriginalFilename(); //获取视频路径
        String videoPath = UUID.randomUUID().toString() + videoPathn;
        File file1 = new File("e:\\exercisefigure\\" + videoPath);
        try {
            videoImgv.transferTo(file);
            videoPathv.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        video.setVideoImg(imgPath);
        video.setVideoPath(videoPath);
        video.setUser(user);
        boolean leep = videoService.addVideo(video);
        if (leep) {
            modelAndView.setViewName("forward:/front/CenTer.jsp");
        } else {
            modelAndView.addObject("result",false);
            modelAndView.setViewName("forward:/front/PublishVideo.jsp");
        }

        modelAndView.addObject("abs",'1');
        return modelAndView;
    }
    @RequestMapping(value = "addVideo2")
    public ModelAndView addVideo2(Video video,
                                 @RequestParam("videoImgv") MultipartFile videoImgv,
                                 @RequestParam("videoPathv") MultipartFile videoPathv,
                                 HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        User user = (User)session.getAttribute("user"); //获取用户
        String videoImgPath = videoImgv.getOriginalFilename(); //获取头像名
        String imgPath = UUID.randomUUID().toString() + videoImgPath;
        File file = new File("e:\\exercisefigure\\" + imgPath);
        String videoPathn = videoPathv.getOriginalFilename(); //获取视频路径
        String videoPath = UUID.randomUUID().toString() + videoPathn;
        File file1 = new File("e:\\exercisefigure\\" + videoPath);
        try {
            videoImgv.transferTo(file);
            videoPathv.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        video.setVideoImg(imgPath);
        video.setVideoPath(videoPath);
        video.setUser(user);
        boolean leep = videoService.addVideo(video);
        if (leep) {
            modelAndView.setViewName("redirect:/frontbg/VideoCenTer.jsp");
        } else {
            modelAndView.addObject("result",false);
            modelAndView.setViewName("redirect:/frontbg/PublishVideo.jsp");
        }
        return modelAndView;
    }
}
